package com.xiaoshuidi.cloud.module.member.repository;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xiaoshuidi.cloud.module.member.controller.admin.programarticle.vo.MemberArticleCreateVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.programarticle.vo.MemberArticlePageVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.programarticle.vo.MemberArticleRespVO;
import com.xiaoshuidi.cloud.module.member.mapper.programarticle.MemberArticleMapper;
import com.xiaoshuidi.cloud.module.member.mapper.programarticle.MemberProgramInfoMapper;
import com.xiaoshuidi.cloud.module.member.pojo.programarticle.MemberArticle;
import com.xiaoshuidi.cloud.module.member.pojo.programarticle.MemberProgramInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xiaoshuidi.cloud.module.infra.enums.ErrorCodeConstants.FILE_IS_EMPTY;
import static com.xiaoshuidi.cloud.module.member.enums.ErrorCodeConstants.PROGRAM_NOT_EXIST;

@Repository
@RequiredArgsConstructor
@Slf4j
public class MemberArticleRepository extends ServiceImpl<MemberArticleMapper, MemberArticle> {
    private final MemberArticleMapper mapper;


    private final MemberProgramInfoMapper programInfoMapper;

    public Boolean create(MemberArticleCreateVO vo) {
         MemberProgramInfo memberProgramInfo = programInfoMapper.selectById(vo.getProgramId());
         if (ObjectUtils.isEmpty(memberProgramInfo)){
             throw exception(PROGRAM_NOT_EXIST);
         }
        MemberArticle memberArticle = new MemberArticle();
        BeanUtils.copyProperties(vo, memberArticle);
        memberArticle.setDeptId(memberProgramInfo.getDeptId());
        mapper.insert(memberArticle);
        return true;
    }

    public Page<MemberArticleRespVO> getPage(MemberArticlePageVO pageVo) {
       return mapper.getPage(new Page<MemberArticle>(pageVo.getPageNo(),pageVo.getPageSize()),pageVo);
    }

    public Boolean edit(MemberArticleCreateVO vo) {
        MemberArticle memberArticle = new MemberArticle();
        BeanUtils.copyProperties(vo, memberArticle);
        mapper.updateById(memberArticle);
        return true;
    }
}
